package com.audaque.springboot.foshanupload.reactor.webfluxdemo.controller;

import com.audaque.springboot.foshanupload.reactor.webfluxdemo.model.pojo.MusicDetail;
import com.audaque.springboot.foshanupload.reactor.webfluxdemo.repository.MusicDetailRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.Objects;
import java.util.Optional;

/**
 * @author zgb
 * @desc ...
 * @date 2023-07-31 01:25:47
 */
@RestController
@RequestMapping("/musicDetail")
public class MusicDetailController {
    @Autowired
    MusicDetailRepository musicDetailRepository;

    @PostMapping("/page")
    public Flux<MusicDetail> page(
            @RequestBody MusicDetail musicDetail,
            @RequestParam(value = "page", defaultValue = "0") long page,
            @RequestParam(value = "size", defaultValue = "10") long size) {
        return musicDetailRepository.findAll()
                .filter(p -> Optional.ofNullable(musicDetail).map(e -> Objects.equals(p.getMusicName(), e.getMusicName())).orElse(true))
                .skip(page * size).take(size);
    }

}
